Page({
    /**
     * 页面的初始数据
     */
    data: {
        musiclist: [],
        listInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this._init(options)
    },

    // 初始化页面：获取歌曲列表
    _init(options) {
        wx.showLoading({
          title: '加载中',
        })
        wx.cloud.callFunction({
            name: 'music',
            data: {
                playlistId: options.playlistId,
                $url: 'musiclist'
            }
        }).then(res => {
            console.log('歌曲列表', res.result.playlist.tracks)
            const playlist = res.result.playlist
            this.setData({
                musiclist: playlist.tracks,
                listInfo: {
                    coverImgUrl: playlist.coverImgUrl,
                    name: playlist.name
                }
            })
            this._setMusiclist()
            wx.hideLoading()
        })
    },

    // 缓存歌曲列表
    _setMusiclist() {
        wx.setStorageSync('musiclist', this.data.musiclist)
    }
})